#include "MarkSprite.h"
#include "ConstClass.h"
#include "PlayerInfo.h"
#include "AudioManage.h"

#define PNG_MARK_COLOR "Mark/mark_color.png"
#define PNG_MARK_RED "Mark/mark_red.png"
#define PNG_MARK_YELLOW "Mark/mark_yellow.png"
#define PNG_MARK_BLUE "Mark/mark_blue.png"
#define PNG_MARK_GREEN "Mark/mark_green.png"
#define PNG_MARK_PURPLE "Mark/mark_purple.png"
#define BMF_NAME_FNT "fonts/helvetica-32.fnt"
#define PNG_DIALOG "Mark/dialog.png"

#define PNG_CARD_0 "Card/card_red_p.png"
#define PNG_CARD_1 "Card/card_yellow_p.png"
#define PNG_CARD_2 "Card/card_blue_p.png"
#define PNG_CARD_3 "Card/card_green_p.png"
#define PNG_CARD_4 "Card/card_purple_p.png"
#define PNG_CARD_5 "Card/card_red_m.png"
#define PNG_CARD_6 "Card/card_yellow_m.png"
#define PNG_CARD_7 "Card/card_blue_m.png"
#define PNG_CARD_8 "Card/card_green_m.png"
#define PNG_CARD_9 "Card/card_purple_m.png"
#define PNG_CARD_10 "Card/card_red_pp.png"
#define PNG_CARD_11 "Card/card_yellow_pp.png"
#define PNG_CARD_12 "Card/card_blue_pp.png"
#define PNG_CARD_13 "Card/card_green_pp.png"
#define PNG_CARD_14 "Card/card_purple_pp.png"
#define PNG_CARD_15 "Card/card_color_p.png"
#define PNG_CARD_16 "Card/card_color_m.png"
#define PNG_CARD_17 "Card/card_last_p.png"
#define PNG_CARD_18 "Card/card_last_pp.png"

#define TIME_FADEIN 0.2f
#define TIME_FADEOUT 0.5f
#define TIME_DELAY 1.0f
#define TIME_TURN 0.5f

MarkSprite::MarkSprite()
{
	this->create();
	this->initWithFile(PNG_MARK_COLOR);
}


MarkSprite::~MarkSprite()
{
}

Layer* MarkSprite::MarksLayer;
Sprite* MarkSprite::dialogSprite;
MarkSprite* MarkSprite::Marks[5];
Sprite* MarkSprite::cardSprite;
Sprite* colorCardSprite;
float MarkSprite::actionTime = TIME_FADEIN + TIME_FADEOUT + TIME_DELAY + TIME_FADEOUT;;

void MarkSprite::StartMarks(Layer* parentLayer)
{
	MarkSprite::MarksLayer = parentLayer;
	dialogSprite = Sprite::create(PNG_DIALOG);
	dialogSprite->setVisible(false);
	parentLayer->addChild(dialogSprite, 5);

	colorCardSprite = Sprite::create();
	colorCardSprite->setScale(0.4f);
	colorCardSprite->setVisible(false);
	parentLayer->addChild(colorCardSprite, 5);

	cardSprite = Sprite::create();
	cardSprite->setScale(0.4f);
	cardSprite->setVisible(false);
	parentLayer->addChild(cardSprite, 5);
	
	Size size = Director::getInstance()->getVisibleSize();
	int playerCount = ConstClass::PLAYER_COUNT;

	for (int i = 0; i < playerCount; i++)
	{
		MarkSprite* mark = new MarkSprite();
		mark->setScale(0.5f);
		mark->setPosition(Vec2(size.width * 0.895f, size.height * (0.82f - 0.13f * i)));
		parentLayer->addChild(mark);

		MarkSprite::Marks[i] = mark;

		Label* nameLabel = Label::createWithBMFont(BMF_NAME_FNT, PlayerInfo::Players[i]->name);
		nameLabel->setPosition(Vec2(size.width * 0.895f, size.height * (0.88f - 0.13f * i)));
		nameLabel->setScale(0.6f);
		parentLayer->addChild(nameLabel);
	}
}

void MarkSprite::playCard(int playerID, int cardID)
{
	Size size = Director::getInstance()->getVisibleSize();
		
	Vec2 markPosition = MarkSprite::Marks[playerID]->getPosition();
	Vec2 dilopPosition = Vec2(markPosition.x - size.width  * 0.2f, markPosition.y);
	Vec2 cardPosition = dilopPosition;
	
	setCardImage(cardID);

	dialogSprite->setPosition(dilopPosition);
	cardSprite->setPosition(cardPosition);

	dialogSprite->runAction(Sequence::create(
		FadeOut::create(0),
		Show::create(),	
		FadeIn::create(TIME_FADEIN),		
		CallFunc::create([]
		{
			AudioManage::cardSound();
			cardSprite->runAction(Sequence::create(
				FadeOut::create(0),
				Show::create(),	
				Spawn::create(
				FadeIn::create(TIME_FADEIN),
				MoveBy::create(TIME_FADEIN, Vec2(-20, 0)),
				NULL),
				DelayTime::create(TIME_DELAY),
				FadeOut::create(TIME_FADEOUT),
				NULL
				));
		}),
		DelayTime::create(TIME_FADEIN + TIME_FADEOUT + TIME_DELAY),
		FadeOut::create(TIME_FADEOUT),
		NULL
		));	
}

void MarkSprite::playCard(int playerID, int colorCardID, int cardID)
{
	Size size = Director::getInstance()->getVisibleSize();

	Vec2 markPosition = MarkSprite::Marks[playerID]->getPosition();
	Vec2 dilopPosition = Vec2(markPosition.x - size.width  * 0.2f, markPosition.y);
	Vec2 cardPosition = dilopPosition;
		
	setCardImage(colorCardID, cardID);

	dialogSprite->setPosition(dilopPosition);
	cardSprite->setPosition(cardPosition);
	colorCardSprite->setPosition(cardPosition);

	dialogSprite->runAction(Sequence::create(
		Show::create(),
		FadeOut::create(0),
		FadeIn::create(TIME_FADEIN),
		CallFunc::create([]
		{
		AudioManage::cardSound();
		colorCardSprite->runAction(Sequence::create(
			Show::create(),
			FadeOut::create(0),
			Spawn::create(
			FadeIn::create(TIME_FADEIN),
			MoveBy::create(TIME_FADEIN, Vec2(-20, 0)),
			NULL),
			DelayTime::create(TIME_DELAY - 0.45f),
			CallFunc::create([]
			{
			AudioManage::cardSound();
			cardSprite->runAction(Sequence::create(
				Show::create(),
				FadeOut::create(0),
				Spawn::create(
				FadeIn::create(TIME_FADEIN - 0.05f),
				MoveBy::create(TIME_FADEIN - 0.05f, Vec2(-20, 0)),
				NULL),
				DelayTime::create(TIME_DELAY - 0.1f),
				FadeOut::create(TIME_FADEOUT - 0.1f),
				NULL
				));
			}),			
			FadeOut::create(TIME_FADEOUT),
			NULL
			));


		}),
		DelayTime::create(TIME_FADEIN + TIME_FADEOUT + TIME_DELAY),
		FadeOut::create(TIME_FADEOUT),
		NULL
		));
}

void MarkSprite::setCardImage(int cardID)
{
	std::string cardImages[19] = { PNG_CARD_0, PNG_CARD_1, PNG_CARD_2, PNG_CARD_3, PNG_CARD_4,
		PNG_CARD_5, PNG_CARD_6, PNG_CARD_7, PNG_CARD_8, PNG_CARD_9,
		PNG_CARD_10, PNG_CARD_11, PNG_CARD_12, PNG_CARD_13, PNG_CARD_14,
		PNG_CARD_15, PNG_CARD_16, PNG_CARD_17, PNG_CARD_18 };
	cardSprite->setTexture(cardImages[cardID]);
}

void MarkSprite::setCardImage(int colorCardID, int cardID)
{
	std::string cardImages[19] = { PNG_CARD_0, PNG_CARD_1, PNG_CARD_2, PNG_CARD_3, PNG_CARD_4,
		PNG_CARD_5, PNG_CARD_6, PNG_CARD_7, PNG_CARD_8, PNG_CARD_9,
		PNG_CARD_10, PNG_CARD_11, PNG_CARD_12, PNG_CARD_13, PNG_CARD_14,
		PNG_CARD_15, PNG_CARD_16, PNG_CARD_17, PNG_CARD_18 };
	cardSprite->setTexture(cardImages[cardID]);
	colorCardSprite->setTexture(cardImages[colorCardID]);
}

void MarkSprite::showTurtles()
{
	for (int i = 0; i < ConstClass::PLAYER_COUNT; i++)
	{
		MarkSprite::Marks[i]->runAction(Sequence::create(
			DelayTime::create(0.2f * i),
			OrbitCamera::create(TIME_TURN, 1, 0, 0, -80, 0, 0),
			CallFunc::create([i]
			{
				std::string markImages[5] = { PNG_MARK_RED, PNG_MARK_YELLOW, PNG_MARK_BLUE,
					PNG_MARK_GREEN, PNG_MARK_PURPLE };

				MarkSprite::Marks[i]->setTexture(markImages[PlayerInfo::Players[i]->colorIndex]);
			}),
			OrbitCamera::create(TIME_TURN, 1, 0, 0, -80, 0, 0)->reverse(),

			NULL
			));
	}
}